﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Web.UI;

namespace TA.Turbo.Controls
{
	[PersistChildren(false)]
	[ParseChildren(true)]
	public class ModalPopupPanel : BasePopupControl
	{
		#region PUBLIC
		public enum BlockingMode
		{
			None = 0, BGOnly = 1, BGAndPopups = 2, All = 3
		}
		public ModalPopupPanel()
		{
			Blocking = BlockingMode.All;

			_header = new SingleInstanceTemplate(this, "Header");
			_footer = new SingleInstanceTemplate(this, "Footer");

			_okControls = new MultiControlValue(this);
			_openControls = new MultiControlValue(this);
			_cancelControls = new MultiControlValue(this);

		}
		#region PROPS
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public SingleInstanceTemplate Header { get { return _header; } }
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public SingleInstanceTemplate Footer { get { return _footer; } }
		public BlockingMode Blocking { get; set; }
		public string OpenArgument { get; set; }
		public string OpenControlIds
		{
			get
			{
				return _openControls.Value;
			}
			set
			{
				_openControls.Value = value;
			}
		}
		public List<Control> OpenControls
		{
			get
			{
				return _openControls.ValueList;
			}
		}
		public string OkControlIds
		{
			get
			{
				return _okControls.Value;
			}
			set
			{
				_okControls.Value = value;
			}
		}
		public List<Control> OkControls
		{
			get
			{
				return _okControls.ValueList;
			}
		}
		public string CancelControlIds
		{
			get
			{
				return _cancelControls.Value;
			}
			set
			{
				_cancelControls.Value = value;
			}
		}
		public List<Control> CancelControls
		{
			get
			{
				return _cancelControls.ValueList;
			}
		}
		public string Title { get; set; }
		#endregion
		#endregion
		#region INTERNAL
		#region VARS
		SingleInstanceTemplate _header, _footer;
		MultiControlValue _okControls, _openControls, _cancelControls;
		#endregion
		#region METHODS
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			TurboPage.Current.Includes.Add(new ScriptResourceInclude
			{
				TypeName = this.GetType().FullName,
				Resource = "TA.Turbo.Resources.JS.ModalPopupPanel.js"
			});

			foreach (Control c in OpenControls)
			{
				if (c is BaseCommandControl)
				{
					((BaseCommandControl)c).Click.Client = GetShowHideScript(true);
				}
			}
			foreach (Control c in CancelControls)
			{
				if (c is BaseCommandControl)
				{
					((BaseCommandControl)c).Click.Client = GetShowHideScript(false);
				}
			}
			foreach (Control c in OkControls)
			{
				if (c is BaseCommandControl)
				{
					BaseCommandControl bc = (BaseCommandControl)c;
					bc.Click.Server += new EventHandler(okControl_Click);
					string psId = bc.Click.PostScenarioId; // need to do this becase AllScenarios changes as the result of getting PostScenarioId
					PostScenario ps = TurboPage.Current.AllScenarios.FirstOrDefault(p => string.Equals(p.Id, psId));
					if (ps != null)
					{
						OneControl oc = ps.ReturnControls.FirstOrDefault(o => o.Control == this);
						if (oc == null)
						{
							ps.ReturnControls.Add(oc = new OneControl
							{
								ControlId = this.UniqueID,
								Value = false
							});
						}
						if (!oc.PropertiesList.Contains("Shown"))
							oc.PropertiesList.Add("Shown");
					}
				}
			}
		}
		void okControl_Click(object sender, EventArgs e)
		{
			if (TurboPage.Current.CurrentScenario.CausesValidation)
			{
				Shown = !Page.IsValid;
			}
			else
				Shown = false;
		}
		protected IEnumerable<Control> GetOpenControls()
		{
			return null;
		}
		protected override void RenderInitScriptInternal(HtmlTextWriter writer)
		{
			writer.Write(string.Format("(new TA.Turbo.Controls.ModalPopupPanel(\"{0}\")).Init();", ClientID));
		}
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			Style[HtmlTextWriterStyle.Position] = "absolute";
			writer.AddAttribute("Blocking", ((int)Blocking).ToString());

			string firstOkButtonId = null;
			if (OkControls.Count > 0)
				firstOkButtonId = OkControls[0].ClientID;
			writer.AddAttribute("FirstOkButtonId", firstOkButtonId);

			base.AddAttributesToRender(writer);
		}
		protected override void RenderChildren(HtmlTextWriter writer)
		{
			// TITLE
			writer.Write(string.Format(
				"<div class=\"title\" onselectstart=\"return false;\">" +
				"<span>{0}</span>" +
				"<a onclick=\"{1}\">&nbsp;</a>" +
				"</div>"
				, Title
				, GetShowHideScript(false)
				));

			// HEADER (help, etc.)
			if (_header.Container.Controls.Count > 0)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Class, "header");
				writer.RenderBeginTag(HtmlTextWriterTag.Div);
				_header.Container.RenderControl(writer);
				writer.RenderEndTag();
			}

			// BODY (help, etc.)
			writer.AddAttribute(HtmlTextWriterAttribute.Class, "body");
			writer.RenderBeginTag(HtmlTextWriterTag.Div);
			Body.Container.RenderControl(writer);
			writer.RenderEndTag();

			// FOOTER (help, etc.)
			if (_footer.Container.Controls.Count > 0)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Class, "footer");
				writer.RenderBeginTag(HtmlTextWriterTag.Div);
				_footer.Container.RenderControl(writer);
				writer.RenderEndTag();
			}
		}
		#endregion
		#endregion
	}
}
